
package main;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GraphicsEnvironment;
import java.awt.Point;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.BorderFactory;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSplitPane;
import javax.swing.KeyStroke;
import javax.swing.SwingUtilities;
import panels.HierarchyPanel;
import panels.OverviewsAndGraphsPanel;

/**
 *
 * @author Raed Shomali
 */
public class COINCOMO extends JFrame implements ActionListener
{
    // Frame Size
    final Dimension DEFAULT_SIZE = new Dimension( 800 , 600 );
    // Frame Location
    final Point DEFAULT_LOCATION = new Point( 100 , 100 );

    // Application
    public static COINCOMO application = null;
    // Container
    Container container = getContentPane();
    // Status Bar Label
    public static JLabel statusBar = new JLabel( " Done." );
    // Split Pane
    JSplitPane splitPane = new JSplitPane();
    // Menus
    JMenu fileMenu = new JMenu( "File" );

    JMenu parametersMenu = new JMenu( "Parameters" );

    JMenu helpMenu = new JMenu( "Help" );
    // File Menu Items
    public static JMenuItem connectDatabaseMenuItem = new JMenuItem( "Connect..." );

    public static JMenuItem disconnectDatabaseMenuItem = new JMenuItem( "Disconnect..." );

    public static JMenuItem newProjectMenuItem = new JMenuItem( "New Project..." );

    public static JMenuItem viewProjectsMenuItem = new JMenuItem( "View Projects..." );
    
    public static JMenuItem viewCotsProjectsMenuItem = new JMenuItem( "View Cots Projects..." );

    public static JMenu exportProjectMenuItem = new JMenu( "Export" );

    JMenuItem exportCVSProjectMenuItem = new JMenuItem( "Export As CSV..." );

    JMenuItem exportHTMLProjectMenuItem = new JMenuItem( "Export As HTML..." );

    JMenuItem exportXMLProjectMenuItem = new JMenuItem( "Export As XML..." );

    JMenuItem exitMenuItem = new JMenuItem( "Exit" );
    
    // Parameters Menu Items
    public static JMenuItem effortAdjustmentFactorsMenuItem = new JMenuItem( "Effort Adjustment Factors" );

    public static JMenuItem scaleFactorsMenuItem = new JMenuItem( "Scale Factors" );

    public static JMenuItem equationEditorMenuItem = new JMenuItem( "Equation Editor" );

    public static JMenuItem functionPointsMenuItem = new JMenuItem( "Function Points" );

    public static JMenuItem personMonthMenuItem = new JMenuItem( "Person Month" );
    // Help Menu Item
    JMenuItem aboutUsMenuItem = new JMenuItem( "About Us..." );
    // Menu Bar that contains all Menus
    JMenuBar menuBar = new JMenuBar();

    public COINCOMO()
    {
        this.setTitle( "USC COINCOMO 2.0" );

        // Default ...
        MenuItemMethods.disconnectFromDatabase( false );

        // ----------------------------------------------------------
        // Label

        // ----------------------------------------------------------
        // Status Bar
        statusBar.setFont( new Font( "courier" , 1 , 12 ) );
        statusBar.setBorder( BorderFactory.createLoweredBevelBorder() );

        // ----------------------------------------------------------
        // Tree

        // ----------------------------------------------------------
        // Split Pane
        splitPane.setDividerLocation( 150 );
        splitPane.setLeftComponent( new HierarchyPanel() );
        splitPane.setRightComponent( new OverviewsAndGraphsPanel() );

        // Refresh Automatically As the Divider is Moving
        splitPane.setContinuousLayout( true );

        // ----------------------------------------------------------
        // Adding Action Listeners To Menu Items
        connectDatabaseMenuItem.addActionListener( this );
        disconnectDatabaseMenuItem.addActionListener( this );
        newProjectMenuItem.addActionListener( this );
        viewProjectsMenuItem.addActionListener( this );
        viewCotsProjectsMenuItem.addActionListener( this );
        exportCVSProjectMenuItem.addActionListener( this );
        exportHTMLProjectMenuItem.addActionListener( this );
        exportXMLProjectMenuItem.addActionListener( this );
        exitMenuItem.addActionListener( this );

        effortAdjustmentFactorsMenuItem.addActionListener( this );
        scaleFactorsMenuItem.addActionListener( this );
        equationEditorMenuItem.addActionListener( this );
        functionPointsMenuItem.addActionListener( this );
        personMonthMenuItem.addActionListener( this );

        aboutUsMenuItem.addActionListener( this );

        // Adding Mnemonics, Icons and Keystrokes to Menu Items
        fileMenu.setMnemonic( 'F' );

        connectDatabaseMenuItem.setMnemonic( 't' );
        connectDatabaseMenuItem.setIcon( Icons.CONNECT_ICON );
        connectDatabaseMenuItem.setAccelerator( KeyStroke.getKeyStroke( "control T" ) );
        disconnectDatabaseMenuItem.setMnemonic( 'D' );
        disconnectDatabaseMenuItem.setIcon( Icons.DISCONNECT_ICON );
        disconnectDatabaseMenuItem.setAccelerator( KeyStroke.getKeyStroke( "control D" ) );
        newProjectMenuItem.setMnemonic( 'N' );
        newProjectMenuItem.setIcon( Icons.NEW_ICON );
        newProjectMenuItem.setAccelerator( KeyStroke.getKeyStroke( "control N" ) );
        viewProjectsMenuItem.setMnemonic( 'W' );
        viewProjectsMenuItem.setIcon( Icons.VIEW_ICON );
        viewProjectsMenuItem.setAccelerator( KeyStroke.getKeyStroke( "control W" ) );
        viewCotsProjectsMenuItem.setMnemonic( 'P' );
        viewCotsProjectsMenuItem.setIcon( Icons.VIEW_ICON );
        viewCotsProjectsMenuItem.setAccelerator( KeyStroke.getKeyStroke( "control P" ) );
        exportProjectMenuItem.setMnemonic( 'E' );
        exportProjectMenuItem.setIcon( Icons.EXPORT_ICON );
        exportCVSProjectMenuItem.setMnemonic( 'C' );
        exportCVSProjectMenuItem.setIcon( Icons.EXPORT_CVS_ICON );
        exportCVSProjectMenuItem.setAccelerator( KeyStroke.getKeyStroke( "shift C" ) );
        exportHTMLProjectMenuItem.setMnemonic( 'H' );
        exportHTMLProjectMenuItem.setIcon( Icons.EXPORT_HTML_ICON );
        exportHTMLProjectMenuItem.setAccelerator( KeyStroke.getKeyStroke( "shift H" ) );
        exportXMLProjectMenuItem.setMnemonic( 'X' );
        exportXMLProjectMenuItem.setIcon( Icons.EXPORT_XML_ICON );
        exportXMLProjectMenuItem.setAccelerator( KeyStroke.getKeyStroke( "shift X" ) );
        exitMenuItem.setMnemonic( 'x' );
        exitMenuItem.setIcon( Icons.EXIT_ICON );
        exitMenuItem.setAccelerator( KeyStroke.getKeyStroke( "alt F4" ) );

        parametersMenu.setMnemonic( 'P' );

        effortAdjustmentFactorsMenuItem.setMnemonic( 'E' );
        effortAdjustmentFactorsMenuItem.setIcon( Icons.EAF_ICON );
        effortAdjustmentFactorsMenuItem.setAccelerator( KeyStroke.getKeyStroke( "F2" ) );
        scaleFactorsMenuItem.setMnemonic( 'S' );
        scaleFactorsMenuItem.setIcon( Icons.SCALE_FACTOR_ICON );
        scaleFactorsMenuItem.setAccelerator( KeyStroke.getKeyStroke( "F3" ) );
        equationEditorMenuItem.setMnemonic( 'Q' );
        equationEditorMenuItem.setIcon( Icons.EQUATION_EDITOR_ICON );
        equationEditorMenuItem.setAccelerator( KeyStroke.getKeyStroke( "F4" ) );
        functionPointsMenuItem.setMnemonic( 'F' );
        functionPointsMenuItem.setIcon( Icons.FUNCTION_POINTS_ICON );
        functionPointsMenuItem.setAccelerator( KeyStroke.getKeyStroke( "F5" ) );
        personMonthMenuItem.setMnemonic( 'M' );
        personMonthMenuItem.setIcon( Icons.PERSON_MONTH_ICON );
        personMonthMenuItem.setAccelerator( KeyStroke.getKeyStroke( "F6" ) );

        helpMenu.setMnemonic( 'H' );

        aboutUsMenuItem.setMnemonic( 'A' );
        aboutUsMenuItem.setIcon( Icons.ABOUT_ICON );
        aboutUsMenuItem.setAccelerator( KeyStroke.getKeyStroke( "F1" ) );

        // Add Each Menu Item To Corresponding Menu
        fileMenu.add( connectDatabaseMenuItem );
        fileMenu.add( disconnectDatabaseMenuItem );
        fileMenu.addSeparator();
        fileMenu.add( newProjectMenuItem );
        fileMenu.add( viewProjectsMenuItem );
        fileMenu.add( viewCotsProjectsMenuItem );
        fileMenu.addSeparator();
        fileMenu.add( exportProjectMenuItem );
        fileMenu.addSeparator();
        fileMenu.add( exitMenuItem );

        exportProjectMenuItem.add( exportCVSProjectMenuItem );
        exportProjectMenuItem.add( exportHTMLProjectMenuItem );
        exportProjectMenuItem.add( exportXMLProjectMenuItem );

        parametersMenu.add( effortAdjustmentFactorsMenuItem );
        parametersMenu.add( scaleFactorsMenuItem );
        parametersMenu.add( equationEditorMenuItem );
        parametersMenu.add( functionPointsMenuItem );
        parametersMenu.add( personMonthMenuItem );

        helpMenu.add( aboutUsMenuItem );

        // Finally Adding the Menus to the Menu Bar
        menuBar.add( fileMenu );
        menuBar.add( parametersMenu );
        menuBar.add( helpMenu );


        // -------------------------------------------------------------
        // Container
        container.setLayout( new BorderLayout() );

        container.add( splitPane );
        container.add( statusBar , BorderLayout.SOUTH );

        // Enable the "Close Button" and Release Memory When Clicked
        this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        
        // In order not to Hide the Task bar when the Frame is Maximized
        GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
        this.setMaximizedBounds( env.getMaximumWindowBounds() );

        // Add The Menus to the Frame
        this.setJMenuBar( menuBar );
        this.setLocation( DEFAULT_LOCATION );

        // Set The Size and Show the Frame
        this.setSize( DEFAULT_SIZE );
        this.setVisible( true );
    }

    public void actionPerformed( final ActionEvent e )
    {
        GlobalMethods.updateStatusBar( "Loading ..." ) ;
        
        // Create a Thread ...
        SwingUtilities.invokeLater( new Runnable()
        {
            public void run()
            {
                if ( e.getSource() == connectDatabaseMenuItem )
                {
                    MenuItemMethods.connectToDatabase();
                }
                else if ( e.getSource() == disconnectDatabaseMenuItem )
                {
                    MenuItemMethods.disconnectFromDatabase( true );
                }
                else if ( e.getSource() == newProjectMenuItem )
                {
                    MenuItemMethods.createNewProject();
                }
                else if ( e.getSource() == viewProjectsMenuItem )
                {
                    MenuItemMethods.ViewProjects();
                }
                /*else if ( e.getSource() == viewCotsProjectsMenuItem )
                {
                    MenuItemMethods.ViewCotsProjects();
                }*/
                else if ( e.getSource() == exportCVSProjectMenuItem )
                {
                    MenuItemMethods.exportProjectAsCVS();
                }
                else if ( e.getSource() == exportHTMLProjectMenuItem )
                {
                    MenuItemMethods.exportProjectAsHTML();
                }
                else if ( e.getSource() == exportXMLProjectMenuItem )
                {
                    MenuItemMethods.exportProjectAsXML();
                }
                else if ( e.getSource() == exitMenuItem )
                {
                    // Exit Program
                    System.exit( 0 );
                }
                else if ( e.getSource() == effortAdjustmentFactorsMenuItem )
                {
                    MenuItemMethods.showEAFDialog();
                }
                else if ( e.getSource() == scaleFactorsMenuItem )
                {
                    MenuItemMethods.showScaleFactorsDialog();
                }
                else if ( e.getSource() == equationEditorMenuItem )
                {
                    MenuItemMethods.showEquationEditorDialog();
                }
                else if ( e.getSource() == functionPointsMenuItem )
                {
                    MenuItemMethods.showFunctionPointsDialog();
                }
                else if ( e.getSource() == personMonthMenuItem )
                {
                    MenuItemMethods.showPersonMonthDialog();
                }
                else if ( e.getSource() == aboutUsMenuItem )
                {
                    MenuItemMethods.showAboutUsDialog();
                }
            }

        } );
    }

    public static void main( String[] args )
    {
        // Create a Nice Look and Feel
        JFrame.setDefaultLookAndFeelDecorated( true );

        // Run Application
        application = new COINCOMO();
    }

}
